package Exercise;

import java.io.*;
import java.util.Scanner;

public class Exercise2 {
    //复制普通文件
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要复制的文件路径:");
        String filePath = sc.next();
        File sourceFile = new File(filePath);
        if(!sourceFile.exists()){
            System.out.println("文件不存在！");
            return;
        }
        if(!sourceFile.isFile()){
            System.out.println("文件不是普通文件!");
            return;
        }
        System.out.println("请输入要复制的目标路径：");
        String destPath = sc.next();
        File destFile = new File(destPath);
        if(destFile.exists()){
            if(destFile.isDirectory()){
                System.out.println("目标路径已存在，并且是一个目录，请确认路径是否正确！");
                return;
            }
            if(destFile.isFile()){
                System.out.println("目标文件已存在，是否进行覆盖? y/n");
                String ans = sc.next();
                if(!ans.equals("y")){
                    System.out.println("复制取消");
                    return;
                }
            }
        }
        try(InputStream inputStream = new FileInputStream(sourceFile);
            OutputStream outputStream = new FileOutputStream(destFile)) {
            while(true){
                byte[] buf = new byte[4096];
                int len = inputStream.read(buf);
                if(len == -1){
                    break;
                }
                System.out.println("len :" + len);
                outputStream.write(buf);
            }
            outputStream.flush();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("复制完成");
    }

}
